package com.zshan.clinic.web.controller.medical;


import com.zshan.clinic.admin.entity.request.GetCancelRegisterListRequest;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.entity.vo.print.ClinicRegisterPrintVo;
import com.zshan.clinic.admin.service.medical.ClinicRegisterService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.ClinicRegister;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 挂号
 */
@RestController
@RequestMapping("/clinic/register")
public class ClinicRegisterController extends AbstractController {


    @Autowired
    private ClinicRegisterService registerService;

    /**
     * 打印：挂号单
     * @param registerId
     * @return
     */
    @GetMapping("/getRegisterPrint")
    public RestResp<ClinicRegisterPrintVo> getRegisterPrint(@RequestParam(name = "registerId") Long registerId) {
        UserInfo userInfo = getUserInfo();
        ClinicRegisterPrintVo clinicRegisterPrintVo = registerService.getRegisterPrint(userInfo.getClinicId(), registerId,userInfo.getEmpName());
        return RestResp.SUCCESS(clinicRegisterPrintVo);
    }

    /**
     * 患者：查询患者挂号记录
     * @param patientId 患者ID
     * @param visitDate 就诊日期
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPatientRegList")
    public RestResp<PageInfo<ClinicRegisterPatientVo>> getPatientRegList(@RequestParam(name = "patientId") Long patientId,
                                                                         @RequestParam(name = "visitDate",required = false) LocalDate visitDate,
                                                                         @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                         @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<ClinicRegisterPatientVo> registerList = registerService.getPatientRegList(userInfo.getClinicId(), patientId,visitDate,pageNo, pageSize);
        return RestResp.SUCCESS(registerList);
    }

    /**
     * 查询挂号列表
     * @param patientName 患者名
     * @param deptId 部门
     * @param visitDate 就诊日期
     * @param regStatus 就诊状态 regStatusEnum
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getRegisterList")
    public RestResp<PageInfo<ClinicRegisterVo>> getRegisterList(@RequestParam(name = "patientName", required = false) String patientName,
                                                          @RequestParam(name = "deptId", required = false) Long deptId,
                                                          @RequestParam(name = "doctorId", required = false) Long doctorId,
                                                          @RequestParam(name = "visitDate") LocalDate visitDate,
                                                          @RequestParam(name = "regStatus", required = false) String regStatus,
                                                          @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        //方便调试
//        if(visitDate == null){
//            visitDate = LocalDate.now();
//        }
        UserInfo userInfo = getUserInfo();
        PageInfo<ClinicRegisterVo> pageInfo = registerService.getRegisterList(userInfo.getClinicId(),patientName, deptId,doctorId,visitDate,regStatus,pageNo, pageSize);
        return RestResp.SUCCESS(pageInfo);
    }

    /**
     * 挂号：查询挂号统计
     * @param patientName
     * @param deptId
     * @param doctorId
     * @param visitDate
     * @return
     */
    @GetMapping("/getRegisterStat")
    public RestResp<StatClinicRegVo> getRegisterStat(@RequestParam(name = "patientName", required = false) String patientName,
                                                     @RequestParam(name = "deptId", required = false) Long deptId,
                                                     @RequestParam(name = "doctorId", required = false) Long doctorId,
                                                     @RequestParam(name = "visitDate") LocalDate visitDate) {
        //方便调试
//        if(visitDate == null){
//            visitDate = LocalDate.now();
//        }
        UserInfo userInfo = getUserInfo();
        StatClinicRegVo statClinicRegVo = registerService.getRegisterStat(userInfo.getClinicId(),patientName, deptId,doctorId,visitDate);
        return RestResp.SUCCESS(statClinicRegVo);
    }

    /**
     * 获取医生当日挂号统计
     * @param visitDate 就诊日期
     * @return
     */
    @GetMapping("/getDoctorList")
    public RestResp<List<DoctorRegisterDailyVo>> getDoctorList(@RequestParam(name = "visitDate") LocalDate visitDate) {
        UserInfo userInfo = getUserInfo();
        List<DoctorRegisterDailyVo> doctorList = registerService.getDoctorList(userInfo.getClinicId(), visitDate);
        return RestResp.SUCCESS(doctorList);
    }


    /**
     * 查询医生余号列表
     * @param doctorName 医生名
     * @param visitDate 就诊日期
     * @return
     */
    @GetMapping("/getRemainList")
    public RestResp<List<DeptRemainVo>> getRemainList(@RequestParam(name = "visitDate") LocalDate visitDate,
                                                      @RequestParam(name = "doctorName",required = false) String doctorName) {
        UserInfo userInfo = getUserInfo();
        List<DeptRemainVo> remainList = registerService.getRemainList(userInfo.getClinicId(),null,visitDate, null,doctorName);
        return RestResp.SUCCESS(remainList);
    }


    /**
     * 挂号
     * @param clinicRegister
     * @return
     */
    @PostMapping("/addRegister")
    public RestResp<RegSuccessVo> addRegister(@RequestBody ClinicRegister clinicRegister) {
        UserInfo userInfo = getUserInfo();
        clinicRegister.setCreateUserId(userInfo.getEmpId());
        clinicRegister.setCreateBy(userInfo.getEmpName());
        clinicRegister.setClinicId(userInfo.getClinicId());
        ClinicRegister dbClinicRegister = registerService.addRegister(clinicRegister);
        RegSuccessVo regSuccessVo = new RegSuccessVo();
        regSuccessVo.setPatientId(dbClinicRegister.getPatientId());
        regSuccessVo.setDeptName(dbClinicRegister.getDeptName());
        regSuccessVo.setDoctorName(dbClinicRegister.getDoctorName());
        regSuccessVo.setVisitStartTime(dbClinicRegister.getVisitStartTime());
        regSuccessVo.setVisitEndTime(dbClinicRegister.getVisitEndTime());
        regSuccessVo.setOrderId(dbClinicRegister.getOrderId());
        regSuccessVo.setRegisterId(dbClinicRegister.getRegisterId());
        regSuccessVo.setPayStatus(dbClinicRegister.getPayStatus());
        return RestResp.SUCCESS(regSuccessVo);
    }

    /**
     * 预约取号
     * @param clinicRegister
     * @return
     */
    @PostMapping("/pickupRegister")
    public RestResp<RegSuccessVo> pickupRegister(@RequestBody ClinicRegister clinicRegister) {
        UserInfo userInfo = getUserInfo();
        clinicRegister.setUpdateUserId(userInfo.getEmpId());
        clinicRegister.setUpdateBy(userInfo.getEmpName());
        clinicRegister.setClinicId(userInfo.getClinicId());
        RegSuccessVo regSuccessVo = registerService.pickupRegister(clinicRegister);
        return RestResp.SUCCESS(regSuccessVo);
    }


    /**
     * 退号
     * @param clinicRegister
     * @return
     */
    @PostMapping("/cancelRegister")
    public RestResp<Void> cancelRegister(@RequestBody ClinicRegister clinicRegister) {
        UserInfo userInfo = getUserInfo();
        clinicRegister.setUpdateUserId(userInfo.getEmpId());
        clinicRegister.setUpdateBy(userInfo.getEmpName());
        clinicRegister.setClinicId(userInfo.getClinicId());
        registerService.cancelRegister(clinicRegister);
        return RestResp.SUCCESS();
    }
}
